var Vbf={
	logger : function (s){console.log(s);},
	log : function (s){if (Vbf.logger) Vbf.logger(s);},
	//get request pararmeter
	  getParameterByName : function(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); },
	getQueryString : function(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); },
	//syntaxHighlight
	syntaxHighlight : function(json) {
	    if (typeof json != 'string') {
		 json = JSON.stringify(json, undefined, 3);
	    }
	    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
	    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
		var cls = 'number';
		if (/^"/.test(match)) {
		    if (/:$/.test(match)) {
		        cls = 'key';
		    } else {
		        cls = 'string';
		    }
		} else if (/true|false/.test(match)) {
		    cls = 'boolean';
		} else if (/null/.test(match)) {
		    cls = 'null';
		}
		return '<span class="' + cls + '">' + match + '</span>';
	    });
	},
	
	//micro template
	cache : {},
 
  	tmpl : function tmpl(str, data){
	    // Figure out if we're getting a template, or if we need to
	    // load the template - and be sure to cache the result.
	    var fn = !/\W/.test(str) ?
	      Vbf.cache[str] = Vbf.cache[str] ||
		tmpl(document.getElementById(str).innerHTML) :
	     
	      // Generate a reusable function that will serve as a template
	      // generator (and which will be cached).
	      new Function("obj",
		"var p=[],print=function(){p.push.apply(p,arguments);};" +
	       
		// Introduce the data as local variables using with(){}
		"with(obj){p.push('" +
	       
		// Convert the template into pure JavaScript
		str
		  .replace(/[\r\t\n]/g, " ")
		  .split("<%").join("\t")
		  .replace(/((^|%>)[^\t]*)'/g, "$1\r")
		  .replace(/\t=(.*?)%>/g, "',$1,'")
		  .split("\t").join("');")
		  .split("%>").join("p.push('")
		  .split("\r").join("\\'")
	      + "');}return p.join('');");
	   
	    // Provide some basic currying to the user
		if ('undefined' === typeof data){};
	    return data ? fn( data ) : fn;
	  },
	//my mvc
	MVC : {
		View : function(view, viewModel){
			var viewPlaceHolder = $(view);
			if (viewPlaceHolder.length ==0) {
				var r = Vbf.tmpl("tmpl_" + view, {"ViewModel" : viewModel});
				viewPlaceHolder = $("#plhd_" + view);
		    		viewPlaceHolder.html(r);
			} 
			$("input[type=checkbox][data-bind]", $(viewPlaceHolder)).each(function(i, cb){
				Vbf.MVC.bindingCheckBox(cb, viewModel);
			});
			
		},
		ViewModel : function(){this.changed = function(cb){}},
		bindingCheckBox : function(cbox, viewModel) {
			var prop = $(cbox).attr('data-bind').split(':');
			var propName = prop[0];
			var defaultValue = false;
			if (prop.length > 1) {defaultValue = prop[1].trim() == 'true';};
			viewModel[propName] = defaultValue;
			$(cbox).prop('checked', viewModel[propName]);
			if($(cbox).prop('bound')) {
			} else {
				$(cbox).prop('bound', true);
				$(cbox).change(function(){
					viewModel[propName] = $(this).prop('checked');
					if (typeof viewModel['changed'] === 'function') { viewModel['changed']($(cbox));}
				});
			}
		},
}
}

$.fn.freezeTable = function() {
	//table.freezedTable thead
	//{
	//    display: block;
	//   position: fixed;
	//}
	//table.freezedTable tbody
	//{
	//    display: block;
        //    margin-top: 30px;
	//}
	this.addClass("freezedTable");
	var headThs = $("thead th", this);
	var bodyTds = $("tbody tr:eq(0) td", this);
	headThs.each(function(i, obj){
	    var th = $(obj);
	    var td = $(bodyTds[i]);
	    if (th.width() > td.width()) {td.width(th.width());}
	    else if (th.width() < td.width()) {th.width(td.width());}
	});
};
$.fn.filterTable = function() {
	for(var i= 0; i < this.length; i++) {
		var th = $(this[i]);
		var tagName = th.tagName;
		if (tagName == "TH") {
			var table = th.closest("table");console.log(th.closest("table"));
		        console.log(table.find("tr td").eq(th.index()));
			th.wrapInner("<div><div></div></div>");
		}
	}
};
